package com.crm.business.crm.service.impl;

import cn.hutool.core.date.DateUtil;
import com.crm.business.crm.mapper.CrmTimeTableMapper;
import com.crm.business.crm.service.ICrmTimeTableService;
import com.crm.common.crm.constant.CrmCodeEnum;
import com.crm.common.crm.entity.PO.CrmTimeTable;
import com.crm.common.crm.entity.PO.CrmTimeTableData;
import com.crm.common.ka.core.entity.UserInfo;
import com.crm.common.ka.core.exception.CrmException;
import com.crm.business.servlet.BaseServiceImpl;
import com.crm.business.utils.UserUtil;
import com.crm.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CrmTimeTableServiceImpl extends BaseServiceImpl<CrmTimeTableMapper, CrmTimeTable> implements ICrmTimeTableService {

    @Autowired
    private CrmTimeTableMapper crmTimeTableMapper;

    @Override
    public List<CrmTimeTable> queryList(CrmTimeTable crmTimeTable) {
        List arrayList=crmTimeTableMapper.queryList(crmTimeTable);

        return arrayList;
    }

    @Override
    public CrmTimeTable queryById(Integer id) {
        return crmTimeTableMapper.selectById(id);
    }

    @Override
    public void addTimeTable(CrmTimeTable crmTimeTable) {
        UserInfo user= UserUtil.getUser();
        crmTimeTable.setCreateTime(DateUtil.date());
        crmTimeTable.setCreateUser(user.getRealname());
        save(crmTimeTable);
    }

    @Override
    public void deleteById(Integer id) {
        crmTimeTableMapper.deleteById(id);
    }

    public void updateClassName(CrmTimeTable crmTimeTable) {
        UserInfo user=UserUtil.getUser();
        crmTimeTable.setUpdateUser(user.getRealname());
        crmTimeTable.setUpdateTime(DateUtil.date());
        updateById(crmTimeTable);
    }

    @Override
    public List<CrmTimeTableData> queryByClassId(Integer id) {
        return crmTimeTableMapper.queryByClassId(id);
    }

    public void addData (CrmTimeTableData crmTimeTableData){
        UserInfo user=UserUtil.getUser();
        crmTimeTableData.setCreateTime(DateUtil.date());
        crmTimeTableData.setCreateUser(user.getRealname());
        crmTimeTableData.getTeacherId();
        String teacherName= crmTimeTableMapper.queryNameById(crmTimeTableData.getTeacherId());
        crmTimeTableData.setTeacherName(teacherName);
        //营销讲师查重
        List<Integer> teacherList = getBaseMapper().queryTeacherByClassId(crmTimeTableData.getClassId());
        if(teacherList.size()>=0) {
            for (int i = 0; i < teacherList.size(); i++) {
                if (crmTimeTableData.getTeacherId().equals(teacherList.get(i))) {
                    throw new CrmException(CrmCodeEnum.CRM_TIME_TABLE_DATA_TEACHER);
                }
            }
        }
        crmTimeTableMapper.addData(crmTimeTableData);
    }

    @Override
    public void deleteData(Integer id) {
        crmTimeTableMapper.deleteData(id);
    }

    @Override
    public void updateData(CrmTimeTableData crmTimeTableData) {
        String teacherName= crmTimeTableMapper.queryNameById(crmTimeTableData.getTeacherId());
        crmTimeTableData.setTeacherName(teacherName);
        //营销讲师查重
        List<Integer> teacherList = getBaseMapper().queryTeacherByClassId(crmTimeTableData.getClassId());
        if(teacherList.size()>=0) {
            for (int i = 0; i < teacherList.size(); i++) {
                if (crmTimeTableData.getTeacherId()!=null && crmTimeTableData.getTeacherId().equals(teacherList.get(i))) {
                    throw new CrmException(CrmCodeEnum.CRM_TIME_TABLE_DATA_TEACHER);
                }
            }
        }
        //期数查重
        if(StringUtils.isNotBlank(crmTimeTableMapper.classArray(crmTimeTableData))){
            String[] array=crmTimeTableMapper.classArray(crmTimeTableData).split(",");
            String[] reqArray=crmTimeTableData.getTrainingPeriods().split(",");
            for(int i=0;i<array.length;i++){
                for (int j=0;j<reqArray.length;j++){
                    if (array[i].equals(reqArray[j])){
                        throw new CrmException(CrmCodeEnum.CRM_TIME_TABLE_DATA_PERIODS);
                    }
                }
            }
        }
        crmTimeTableMapper.updateData(crmTimeTableData);
    }

}
